package com.example.demo.Orc.tencent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.HttpUtil;

/**
 * @Author: zjq
 * @Date: create in 2023/5/18 10:13
 */
public class orc {

    private static String grant_type = "client_credential";
    //应用凭证
    private static String appid = "wx18bf5c7b46d6a200";
    //appsecret
    private static String secret = "316e0de09380887583cf6c449516dc2e";


    //企业凭证，两小时一更新
    private static String accessToken = null;
    //过期时间
    private static Long expiresIn = null;
    //错误码
    private static String errcode = null;

    //获取token,每7200秒失效
    public static String gettoken() {
        String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&"
                + "appid=" + appid + "&secret=" + secret);

        JSONObject json = JSON.parseObject(result);
        System.out.println("请求接口返回的json" + json);

        if (!json.getString("access_token").isEmpty()) {
            accessToken = json.getString("access_token");
            expiresIn = json.getLong("expires_in");
            System.out.println("accessToken" + accessToken);
            System.out.println("expiresIn" + expiresIn);
            return accessToken;
        } else {
            errcode = json.getString("errcode");
            System.out.println("errcode" + errcode);
            return errcode;
        }
    }

    public static void main(String[] args) {
        gettoken();
    }
}
